package com.example.lj.uav.utils;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Environment;
import android.view.View;

import java.io.File;
import java.io.FileOutputStream;

public class ScreenShootUtils {

    public static boolean screenShoot(Activity activity){
        try {
            View view = activity.getWindow().getDecorView();
            view.setDrawingCacheEnabled(true);
            view.buildDrawingCache();
            Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
            if (bitmap!=null){
                String path = "";
                if (Environment.getExternalStorageState().equals(
                        Environment.MEDIA_MOUNTED)) {
                    // 优先保存到SD卡中
                    path = Environment.getExternalStorageDirectory()
                            .getAbsolutePath() + File.separator + "HEQ/payload_screen_shoot";
                } else {
                    // 如果SD卡不存在，就保存到本应用的目录下
                    path = activity.getFilesDir().getAbsolutePath()
                            + File.separator + "HEQ";
                }
                File file = new File(path);
                if (!file.exists()) {
                    file.mkdirs();
                }
                String imagePath = path+"/payload_"+
                        MyDate.getTime() +
                        ".png";
                File imageFile = new File(imagePath);
                if (!imageFile.exists()) {
                    imageFile.createNewFile();
                }
                FileOutputStream os = new FileOutputStream(imageFile);
                bitmap.compress(Bitmap.CompressFormat.PNG,100,os);
                os.flush();
                os.close();

            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
